
package com.shop.cloud.upms.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.upms.admin.service.SysTenantPackageCostService;
import com.shop.cloud.upms.admin.service.SysTenantService;
import com.shop.cloud.upms.common.entity.SysTenant;
import com.shop.cloud.upms.common.entity.SysTenantPackageCost;
import com.shop.cloud.upms.common.entity.SysTenantPackageRecharge;
import com.shop.cloud.upms.admin.mapper.SysTenantPackageRechargeMapper;
import com.shop.cloud.upms.admin.service.SysTenantPackageRechargeService;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.boot.autoconfigure.klock.model.LockTimeoutStrategy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 租户套餐充值记录
 *
 * @author
 * @date 2023-12-27 11:17:30
 */
@Service
@AllArgsConstructor
public class SysTenantPackageRechargeServiceImpl extends ServiceImpl<SysTenantPackageRechargeMapper, SysTenantPackageRecharge> implements SysTenantPackageRechargeService {

    private final SysTenantPackageCostService sysTenantPackageCostService;
    private final SysTenantService sysTenantService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Klock(name = "notifyTenantPackageRecharge", keys = {"#sysTenantPackageRecharge.getId()"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
    public void notifyTenantPackageRecharge(SysTenantPackageRecharge sysTenantPackageRecharge) {
        if(CommonConstants.NO.equals(sysTenantPackageRecharge.getIsPay())){
            sysTenantPackageRecharge.setIsPay(CommonConstants.YES);
            this.updateById(sysTenantPackageRecharge);
            SysTenantPackageCost sysTenantPackageCost = sysTenantPackageCostService.getById(sysTenantPackageRecharge.getPackageCostId());
            //判断是更新套餐还是旧套餐续费；租户过期时间：更新套餐则从当前时间重新算，旧套餐续费则在原过期时间上直接累加
            SysTenant sysTenant = sysTenantService.getById(sysTenantPackageRecharge.getTenantId());
            if(sysTenant.getPackageId() != null && sysTenant.getPackageId().equals(sysTenantPackageRecharge.getPackageId())){
                //旧套餐续费
                if(CommonConstants.PACKAGE_COST_TYPE_1.equals(sysTenantPackageCost.getType())){
                    //按天
                    sysTenant.setExpireTime(sysTenant.getExpireTime().plusDays(sysTenantPackageRecharge.getCount()));
                }
                if(CommonConstants.PACKAGE_COST_TYPE_2.equals(sysTenantPackageCost.getType())){
                    //按月
                    sysTenant.setExpireTime(sysTenant.getExpireTime().plusMonths(sysTenantPackageRecharge.getCount()));
                }
                if(CommonConstants.PACKAGE_COST_TYPE_3.equals(sysTenantPackageCost.getType())){
                    //按年
                    sysTenant.setExpireTime(sysTenant.getExpireTime().plusYears(sysTenantPackageRecharge.getCount()));
                }
                //更新过期时间
                sysTenantService.updateById(sysTenant);
            }else{
                //新租户或更新套餐
                sysTenant.setPackageId(sysTenantPackageRecharge.getPackageId());
                if(CommonConstants.PACKAGE_COST_TYPE_1.equals(sysTenantPackageCost.getType())){
                    //按天
                    sysTenant.setExpireTime(LocalDateTime.now().plusDays(sysTenantPackageRecharge.getCount()));
                }
                if(CommonConstants.PACKAGE_COST_TYPE_2.equals(sysTenantPackageCost.getType())){
                    //按月
                    sysTenant.setExpireTime(LocalDateTime.now().plusMonths(sysTenantPackageRecharge.getCount()));
                }
                if(CommonConstants.PACKAGE_COST_TYPE_3.equals(sysTenantPackageCost.getType())){
                    //按年
                    sysTenant.setExpireTime(LocalDateTime.now().plusYears(sysTenantPackageRecharge.getCount()));
                }
                //更新过期时间
                sysTenantService.updateById(sysTenant);
                //更换套餐要同步套餐权限
                sysTenantService.syncTenantPackage(sysTenantPackageRecharge.getTenantId());
            }
        }
    }
}
